#!/usr/bin/env bash

set -eo pipefail

MULTI_RESOURCES="$(cd "$(dirname "$0")"; pwd -P)"
MULTI_APP_BUNDLE_ID="$(echo "$MULTI_APP_NAME" | sed -e 's/[^a-zA-Z0-9.]/-/g')"
MULTI_APP_LOCATION="$MULTI_RESOURCES/../../../Multi/$MULTI_APP_NAME.app"
MULTI_ICON_TMP_DIR="$(mktemp -d)/Icon.iconset"

if [[ -z "$MULTI_APP_NAME" ]]
then
  echo "USAGE: MULTI_APP_NAME=xxx $(basename "$0")"
  echo "Create a custom, lightweight macOS app from a group of websites."
  echo "All options are passed as environment variables:"
  echo ""
  echo "  MULTI_APP_NAME     Required"
  echo "  MULTI_ICON_PATH    Optional  PNG or ICNS path to icon image"
  echo "  MULTI_JSON_CONFIG  Optional  Configuration as a JSON string"
  echo "  MULTI_OVERWRITE    Optional  Set to '1' to replace an existing Multi app with the same name"
  echo ""
  echo "More documentation at <https://github.com/kofigumbs/multi#using-the-cli-create-mac-app>"
  exit 1

elif [[ -e "$MULTI_APP_LOCATION" ]]
then
  if [[ "$MULTI_OVERWRITE" -ne "1" ]]
  then
    echo "ERROR: \"$MULTI_APP_LOCATION\" already exists!"
    if [[ -z "$MULTI_UI" ]]
      then echo "Rerun with MULTI_OVERWRITE=1 to replace it."
      else echo "You can make this change via the preferences in \"$MULTI_APP_NAME\""
    fi
    exit 1
  fi

else
  mkdir -p "$MULTI_APP_LOCATION/Contents/Resources"
  chmod +x "$MULTI_APP_LOCATION"
fi

mkdir -p "$MULTI_APP_LOCATION/Contents/MacOS"
cp "$MULTI_RESOURCES/../MacOS/Stub" "$MULTI_APP_LOCATION/Contents/MacOS/Stub"
cp "$MULTI_RESOURCES/Stub.plist" "$MULTI_APP_LOCATION/Contents/Info.plist"
sed -e "s/{{name}}/$MULTI_APP_NAME/g" -e "s/{{id}}/$MULTI_APP_BUNDLE_ID/g" -i '' "$MULTI_APP_LOCATION/Contents/Info.plist"

if [[ -z "$MULTI_JSON_CONFIG" ]]
then
  echo "Skipping config update because no JSON provided"
else
  echo "$MULTI_JSON_CONFIG" > "$MULTI_APP_LOCATION/Contents/Resources/config.json"
fi

if [[ "$MULTI_ICON_PATH" == *.icns ]]
then
  cp "$MULTI_ICON_PATH" "$MULTI_APP_LOCATION/Contents/Resources/Icon.icns"
elif [[ "$MULTI_ICON_PATH" == *.png ]]
then
  mkdir "$MULTI_ICON_TMP_DIR"
  sips -z 16   16   "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_16x16.png"      > /dev/null
  sips -z 32   32   "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_16x16@2x.png"   > /dev/null
  sips -z 32   32   "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_32x32.png"      > /dev/null
  sips -z 64   64   "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_32x32@2x.png"   > /dev/null
  sips -z 128  128  "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_128x128.png"    > /dev/null
  sips -z 256  256  "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_128x128@2x.png" > /dev/null
  sips -z 256  256  "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_256x256.png"    > /dev/null
  sips -z 512  512  "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_256x256@2x.png" > /dev/null
  sips -z 512  512  "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_512x512.png"    > /dev/null
  sips -z 1024 1024 "$MULTI_ICON_PATH" --out "$MULTI_ICON_TMP_DIR/icon_512x512@2x.png" > /dev/null
  iconutil -c icns --output "$MULTI_APP_LOCATION/Contents/Resources/Icon.icns" "$MULTI_ICON_TMP_DIR"
else
  echo "Skipping icon creation because no PNG or ICNS provided"
fi

touch "$MULTI_APP_LOCATION"
codesign --sign - --force "$MULTI_APP_LOCATION"

if [[ ! -z "$MULTI_RELAUNCH_PID" ]]
then
  kill "$MULTI_RELAUNCH_PID"
  open -n "$MULTI_APP_LOCATION" || true
fi
